package dao;// AttendanceDAO.java
import pojo.Attendance;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class AttendanceDAO {

    // 录入考勤信息
    public boolean addAttendance(Attendance att) {
        String sql = "INSERT INTO attendance(employee_id, attendance_date, status) VALUES (?, ?, ?)";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, att.getEmployeeId());
            pstmt.setDate(2, new java.sql.Date(att.getDate().getTime()));
            pstmt.setString(3, att.getStatus());
            int affected = pstmt.executeUpdate();
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 根据员工编号和日期范围查询考勤记录
    public List<Attendance> queryAttendance(String employeeId, Date startDate, Date endDate) {
        List<Attendance> list = new ArrayList<>();
        String sql = "SELECT * FROM attendance WHERE employee_id = ? AND attendance_date BETWEEN ? AND ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, employeeId);
            pstmt.setDate(2, new java.sql.Date(startDate.getTime()));
            pstmt.setDate(3, new java.sql.Date(endDate.getTime()));
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Attendance att = new Attendance(
                        rs.getString("employee_id"),
                        rs.getDate("attendance_date"),
                        rs.getString("status")
                );
                list.add(att);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 查询所有考勤记录
    public List<Attendance> getAllAttendance() {
        List<Attendance> list = new ArrayList<>();
        String sql = "SELECT * FROM attendance";
        try (Connection conn = DBConnection.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Attendance att = new Attendance(
                        rs.getString("employee_id"),
                        rs.getDate("attendance_date"),
                        rs.getString("status")
                );
                list.add(att);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 统计指定日期范围内该员工的考勤情况（例如：迟到次数、旷工天数）
    public Map<String, Integer> getAttendanceStatistics(String employeeId, Date startDate, Date endDate) {
        Map<String, Integer> stats = new HashMap<>();
        stats.put("迟到", 0);
        stats.put("旷工", 0);
        // 可根据需要扩展其他状态的统计（例如早退、请假等）
        List<Attendance> list = queryAttendance(employeeId, startDate, endDate);
        for (Attendance att : list) {
            String status = att.getStatus();
            if ("迟到".equals(status)) {
                stats.put("迟到", stats.get("迟到") + 1);
            } else if ("旷工".equals(status)) {
                stats.put("旷工", stats.get("旷工") + 1);
            }
        }
        return stats;
    }
}
